package net.huashitong.Project.service;

import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Project.entity.EarlyWarningNode;
import net.huashitong.Project.filter.EarlyWarningNodeFilter;
import net.huashitong.node.entity.NodeNewschedules;
import net.huashitong.node.service.NodeNewschedulesService;
import net.huashitong.user.service.MessageService;
import net.huashitong.user.service.SuperviseUserService;
import net.huashitong.user.service.TemporaryMessageService;
import org.apache.shiro.crypto.hash.Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class NewEarlyWarningService {
    @Autowired
    private NodeNewschedulesService nss;
    @Autowired
    private NewProjectsService nService;
    @Autowired
    private NodeNewschedulesService nodeNewschedulesService;
    @Autowired
    private EarlyWarningNodeService ewns;
    @Autowired
    private MessageService ms;
    @Autowired
    private TemporaryMessageService tms;
    @Autowired
    private SuperviseUserService superviseUserService;


    public Map<String, Integer> queryByWarningNumber(String projectId) {
        EarlyWarningNodeFilter filter = new EarlyWarningNodeFilter();
        if (StringUtils.isNotBlank(projectId)) {
            int num = 0;
            int total;
            int low = 0;
            int mid = 0;
            int high = 0;
            filter.setProjectId(projectId);
            List<EarlyWarningNode> earlyWarningNodes = ewns.queryEarlyWarning(filter);
            total = earlyWarningNodes.size();
            for (EarlyWarningNode ewn : earlyWarningNodes) {
                num = Integer.parseInt(ewn.getNumber());
                if (num > 0 && num <= 5) {
                    low += 1;
                }
                if (num > 5 && num <= 10) {
                    mid += 1;
                }
                if (num > 10) {
                    high += 1;
                }
            }
            Map<String, Integer> map = new HashMap<>();
            map.put("low", low);
            map.put("mid", mid);
            map.put("high", high);
            map.put("total", total);
            return map;
        } else {
            return null;
        }
    }

    public List<EarlyWarningNode> queryWarningDetail(String projectId, String level) {
        EarlyWarningNodeFilter filter = new EarlyWarningNodeFilter();
        if (StringUtils.isNotBlank(projectId) && StringUtils.isNotBlank(level)) {
            filter.setProjectId(projectId);
            List<EarlyWarningNode> earlyWarningNodes = ewns.queryEarlyWarning(filter);
            int num = 0;
            List<EarlyWarningNode> low = new ArrayList<>();
            List<EarlyWarningNode> mid = new ArrayList<>();
            List<EarlyWarningNode> high = new ArrayList<>();
            List<EarlyWarningNode> resList = new ArrayList<>();

            for (EarlyWarningNode ewn : earlyWarningNodes) {
                num = Integer.parseInt(ewn.getNumber());
                if (num > 0 && num <= 5) {
                    low.add(ewn);
                }
                if (num > 5 && num <= 10) {
                    mid.add(ewn);
                }
                if (num > 10) {
                    high.add(ewn);
                }
            }
            if (level.equals("1")) {
                for (EarlyWarningNode ewn : low) {
                    String nodeId = ewn.getNodeId();
                    for(EarlyWarningNode e : earlyWarningNodes){
                        if(nodeId.equals(e.getNodeId())){
                            resList.add(e);
                        }
                    }

//                    List<EarlyWarningNode> earlyWarnings =  ewns.queryEarlyWarning(filter);
//                    smallNodes.addAll(nodeNewschedulesService.getNode3(nodeId, projectId));
                }
            }
            if (level.equals("2")) {
                for (EarlyWarningNode m : mid) {
                    String nodeId = m.getNodeId();
                    for(EarlyWarningNode e : earlyWarningNodes){
                        if(nodeId.equals(e.getNodeId())){
                            resList.add(e);
                        }
                    }
//                    smallNodes.addAll(nodeNewschedulesService.getNode3(nodeId, projectId));
                }
            }
            if (level.equals("3")) {
                for (EarlyWarningNode h : high) {
                    String nodeId = h.getNodeId();
                    for(EarlyWarningNode e : earlyWarningNodes){
                        if(nodeId.equals(e.getNodeId())){
                            resList.add(e);
                        }
                    }
//                    smallNodes.addAll(nodeNewschedulesService.getNode3(nodeId, projectId));
                }
            }
            return resList;
        } else {
            return null;
        }
    }

    public List<EarlyWarningNode> queryByWarningNumberShort(String projectId) {
        EarlyWarningNodeFilter filter = new EarlyWarningNodeFilter();
        if (StringUtils.isNotBlank(projectId)) {
            filter.setProjectId(projectId);
            List<EarlyWarningNode> earlyWarningNodes = ewns.queryEarlyWarning(filter);
            return earlyWarningNodes;
        } else {
            return null;
        }
    }
}

